﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.TagHelpers.Internal;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.FileProviders;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace StaticFilesSample.Infrastructure.Extensions
{
    public static class RazorPageExt
    {
        public static string AddAppendVersion(this RazorPage page, string path)
        {
            var ctx = page.ViewContext.HttpContext;
            var env = ctx.RequestServices.GetService(typeof(IHostingEnvironment)) as IHostingEnvironment;
            var cache = ctx.RequestServices.GetService(typeof(IMemoryCache)) as IMemoryCache;

            var provider = new FileVersionProvider(new PhysicalFileProvider(env.ContentRootPath), cache, "/OutputStaticFiles");
            return provider.AddFileVersionToPath(path);
        }
    }
}
